<?php
/**
 * Created by PhpStorm.
 * User: zc
 * Date: 2019/8/5
 * Time: 23:36
 */
namespace app\api\controller\v1;


use app\common\controller\Api;
use addons\epay\library\Service;
use app\common\logic\CourseOrder;
use app\common\model\course\Course;
use app\common\model\course\Order;
use Exception;
use fast\Random;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;
use think\config\driver\Json;
use think\Db;

class Pay extends Api
{

    protected $noNeedLogin = ['notify'];  //不能登陆方法
    protected $noNeedRight = '*';


    //小程序支付 购买课程
    public function pay() {
        $id = $this->request->request("id");
        $course = Course::get($id);
        if (!$course) {
            $this->error('课程信息出错');
        }
        $coupon_id = $this->request->request("coupon_id")?:0;//优惠券id
        $discount = Db::name('coupon')->where('id', $coupon_id)->value('money', 0);
        $user = $this->auth->getUser();
        $openid = Db::name('third')->where('id', $user['id'])->value('openid');
//        $out_trade_no = MD5(time().UID.Random::alnum(4));
        $out_trade_no = date('YmdHis').'10'.Random::alnum(10);
        if ($user['user_type'] == 2) {
            $amount = $course['visitor_price'] - $discount;
        } else {
            $amount = $course['price'] - $discount;
        }

        $orderData = [
            'learn_course_id'=> $id,
            'coupon_id'=> $coupon_id,
            'price' => $amount,
            'order_sn'=> $out_trade_no,
            'org_cate_id' => ORG_CATE_ID
        ];

        $order = new CourseOrder();
        $order->save($orderData);
        Db::name('coupon_user')->where(['user_id'=> UID,'coupon_id'=> $coupon_id])->update(['status'=>2]);
        Db::name('coupon')->where(['id' => $coupon_id])->setInc('use_time', 1);
        $params = [
            'type'         => 'wechat',
            'orderid' => $out_trade_no,
            'title'        => '购买课程',
            'amount'       => $amount,
            'method'       => 'miniapp',
            'openid'       => $openid,
            'auth_code'    => '',
            'notifyurl'    => request()->root(true) . '/api/v1.pay/notify',
            'returnurl'    => '',
        ];

        $res = Service::submitOrder($params);
        $this->success('pay', $res);


    }

    //支付回调
    public function notify() {
        $pay = \addons\epay\library\Service::checkNotify('wechat');
        if (!$pay) {
            echo '签名错误';
            return;
        }

        $data = $pay->verify();
        try {
            $out_trade_no = $data['out_trade_no'];;
            //你可以在此编写订单逻辑
            $order = Order::get(['order_sn' => $out_trade_no]);
            if ($order && $order->type == 1) {
                echo $pay->success();
                return;
            }

            $order->type = 1;
            $order->pay_time = time();
            $order->save();

            Db::name('learn_course')->where(['id'=> $order['learn_course_id']])->setInc('buy_numbers', 1);
        } catch (Exception $e) {
            echo $e->getMessage();
            return;
        }
        echo $pay->success();
    }
}